<?php

/**
 *  This script returns a JSON string containing the friends and others with searched tag.
 *  
 * Parameters:
 * [MANDATORY]  	[tag]     	[string]   	Tag to search.
 * [OPTIONAL]  		[distance]  [float]   	The maximum distance for interesting people. Default is 50 (km).
 *  
 *  OUTPUT:
 *  JSON string representation of an 2 element associative array with keys "friends" and "others". 
 *  Each element of the "friends" array and the "others" element has the following fields:
 *  <li>uid			[string]	- ID of the person 
 *  <li>name        [string]	- Name of the person
 *  <li>pic_square 	[string]	- Profile picture of the person
 *  <li>timestamp	[string]	- Message timestamp in Unix timestamp format (seconds since '1970-01-01 00:00:00' UTC)
 *  <li>tags        [string]	- A comma delimited list of tags the person has
 *
 *  The "friends" array contains also additional fields:
 *  <li>lat   		[float]	    - Friends's location latitude value
 *  <li>lon    		[float]     - Friends's location longitude value
 *  <li>address     [string]    - Friends's location address
 *  <li>note        [string]    - Friends's status message
 *  <li>distance    [float]     - Distance to the friend 
 * 
 *  Note: the [string] format means JSON's string format, so including the white and special char formattings,
 *  e.g. '/' -> '\/', '\' -> '\\', ...etc. See http://www.json.org/ for the full specification.
 */
header('Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');

try {
    include_once './log4php/Logger.php';
    include_once './User.php';
    include_once './Situare.php';
    include_once './I18N.php';
    include_once './Error.php';
    include_once './Utils.php';
    include_once './constants.php';

    // initialize logging
    Logger::configure('./log4j.properties');

    $log = Logger::getLogger(basename(__FILE__));
    $log->debug('[START] ' . basename(__FILE__));

    $START_TIME = microtime(TRUE);

    // read the input params
    $inputArray = Utils::getInputParameters();
    $api = Utils::getInputParameterValue($inputArray, PARAM_API, NULL);	
    $tag = Utils::getInputParameterValue($inputArray, PARAM_TAG, NULL);
	$distance = Utils::getInputParameterValue($inputArray, PARAM_DISTANCE, NULL);
	
    $situare = new Situare();    
    
    // in case of invalid session
    if (!$situare->isValidSession()) {
        $log->debug('$_COOKIE[] = ' . Utils::toString($_COOKIE));
        $log->debug('$_POST[] = ' . Utils::toString($_POST));
        $log->debug('$_GET[] = ' . Utils::toString($_GET));
        throw new SituareException(
                "Not found valid session data!",
                ERROR_SESSION_INVALID_OR_MISSING);
    }

	// get the friends and others
	$friendsArray = $situare->getFriendsWithTag($distance, $tag);
	$othersArray = $situare->getOthersWithTag($distance, $tag);

    // build up the result array containing friends and others
    $resultArray = array();
    $resultArray['friends'] = $friendsArray;
    $resultArray['others'] = $othersArray;

    // create JSON string from the result array and return it
    $jsonStr = json_encode($resultArray);
    
    if ($api == API_2_0) {
        $response = Situare::getApi20Response($resultArray);
        echo $response;
        $log->debug('Result JSON string = ' . $response);
    }
    else {
        echo $jsonStr;
        $log->debug('Result JSON string = ' . $jsonStr);
    }
    
    $log->debug('[ END ] ' . basename(__FILE__));

    $END_TIME = microtime(TRUE);
    $log->debug('[PERF ] Total script execution took ' . ($END_TIME - $START_TIME) . "ms");
} catch (Exception $e) {
    $log->error("Exception:");
    $log->error("Code: " . $e->getCode());
    $log->error("Message: " . $e->getMessage());
    $log->error("Location: " . $e->getFile() . " at line " . $e->getLine());
    $log->error("Trace: " . $e->getTraceAsString());

    if ($api == API_2_0) {
        echo Situare::getApi20Response($e);
    }
    else  {
        echo getExceptionAsJson($e);
    }
}



?>
